package org.jeecg.modules.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yaorange.jxc.goods.entity.Goods;
import com.yaorange.jxc.goods.entity.GoodsType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.api.vo.PageVo;
import org.jeecg.modules.service.GoodsTypeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: Andy.Li
 * @Date: 2022.04.28 11:23:10
 */
@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {
    @Resource
    private GoodsTypeService goodsTypeService;

    //获取各个节点
    @PostMapping("/goodsType")
    public ResponseEntity<?> getGoodsType(@RequestBody String body) {

        List<GoodsType> list = goodsTypeService.selectGoodsType(body);
        return ResponseEntity.ok(list);
    }



    //获取所有商品列表
    @GetMapping("/goodsList")
    public ResponseEntity<?> getGoodsList(@RequestParam(value = "current",defaultValue = "1") Long current,
                                          @RequestParam(value = "pageSize",defaultValue = "5") Long pageSize,
                                          String goodsName,
                                          Integer goodsTypeId
    ){
        PageVo<Goods> pageVo=goodsTypeService.getAllGoodsList(current,pageSize,goodsName,goodsTypeId);
        return ResponseEntity.ok(pageVo);
    }

    //查询单个商品
    @PostMapping("/getGoodsById")
    public ResponseEntity<?> getGoodsById(@RequestBody String body){
        if (StringUtils.isEmpty(body)){
            return ResponseEntity.badRequest().build();
        }

        //解析json
        JSONObject object = JSONObject.parseObject(body);
        String id = object.getString("goodsId");

        if (StringUtils.isEmpty(id)){
            return ResponseEntity.badRequest().build();
        }

        //查询

        return ResponseEntity.ok( goodsTypeService.selectGoodsById(Integer.parseInt(id)));


    }
}
